<template>
  <div>
    <div class="big-box">
      <div class="small-box">
        <div class="container" v-for="(item, index) of treeData" :key="index">
          <div
            class="grade-box"
            :style="{
              height: box_height,
              margin: box_margin,
              width: box_width,
              background: rank_backColor[index],
            }"
          >
            {{ item.rank }}
          </div>
          <div
            class="data-box"
            :style="{
              height: box_height,
              margin: box_margin,
              width: box_width,
            }"
          >
            <div>排名</div>
            <div>区县</div>
            <div>得分</div>
            <div>比率</div>
          </div>

          <div
            class="data-box"
            v-for="(data, index) of treeData[index].datas"
            :key="index"
            :style="{
              height: box_height,
              margin: box_margin,
              width: box_width,
            }"
          >
            <div>{{ index + 1 }}</div>
            <div>
              {{ data.district_name }}
            </div>
            <div>{{ Math.round(data.target_ratio * 100) / 100 }}</div>
            <div>{{ data.target_score }}</div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import $ from "jquery";

export default {
  data() {
    return {
      treeData: [],
      box_margin: null,
      box_height: null,
      box_width: null,
      bixBox_height: 832,
      bigBox_width: 972,
      rank_backColor: [
        "rgb(30, 164, 101)",
        "rgb(57,119,178)",
        "rgb(226,196,40)",
        "rgb(230,145,23)",
        "rgb(117,67,50)",
      ],
    };
  },
  mounted() {
    this.getInitData();
  },
  computed: {},
  methods: {
    getInitData() {
      $.getJSON("../data/txt/data2.json", (data) => {
        data.map((data) => {
          this.treeData.push({
            rank: data.district[0].target_level,
            datas: data.district,
          });
        });
        console.log(this.treeData);

        let dataLength = 8;
        this.treeData.map((data) => {
          dataLength += data.datas.length;
        });

        this.box_height = (this.bixBox_height * 3) / dataLength;
        this.box_margin = "0 " + Math.round(this.box_height * 0.1);
        this.box_width =
          this.bigBox_width / 3 - Math.round(this.box_height * 0.1) * 2;

        this.box_height = this.box_height + "px";
        this.box_margin = this.box_margin + "px";
        this.box_width = this.box_width + "px";
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.big-box {
  position: absolute;
  right: 20%;
  top: 3%;
  transform: translate(-50% -50%);
  width: 972px;
  height: 832px;
  background-color: rgb(6, 45, 102);

  .small-box {
    width: 100%;
    height: 100%;
    display: flex;
    flex-wrap: wrap;
    flex-direction: column;
  }

  .container {
    display: flex;
    flex-wrap: wrap;
    flex-direction: column;
  }

  .data-box {
    display: flex;
    flex-direction: row;
    color: white;

    div {
      display: flex;
      justify-content: center;
      align-items: center;
    }

    div:nth-child(1) {
      width: 20%;
    }

    div:nth-child(2) {
      width: 40%;
    }
    div:nth-child(3) {
      width: 20%;
    }
    div:nth-child(4) {
      width: 20%;
    }
  }

  .data-box:nth-child(odd) {
    background-color: rgb(13, 53, 105);
  }

  .data-box:nth-child(even) {
    background-color: rgb(20, 59, 114);
  }
}

.grade-box {
  display: flex;
  justify-content: center;
  align-items: center;
  width: 33%;
  color: white;
  background-color: rgb(30, 164, 101);
}
</style>